Swadhin Biswas feat(api): add runner, analytics, packages, and security services · 3/6/2026
28f7954
74 lines · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import type { APIRoute } from "astro";
import { z } from "zod";
import { withErrorHandler } from "@/lib/errors";
import { badRequest, forbidden, notFound, parseBody, success, unauthorized } from "@/lib/api";
import { pluginManager } from "@/lib/plugins";

const patchSchema = z.object({
  action: z.enum(["enable", "disable", "reload", "unload"]),
  config: z.record(z.unknown()).optional(),
});

export const GET: APIRoute = withErrorHandler(async ({ locals, params }) => {
  const user = locals.user;
  if (!user) return unauthorized();
  if (!user.isAdmin) return forbidden();

  const name = params.name;
  if (!name) return badRequest("Missing plugin name");

  const plugin = pluginManager.getPlugin(name);
  if (!plugin) return notFound("Plugin not found");

  return success({
    plugin,
    runtime: pluginManager.getPluginState(name) || null,
    config: pluginManager.getPluginConfig(name),
  });
});

export const PATCH: APIRoute = withErrorHandler(async ({ locals, params, request }) => {
  const user = locals.user;
  if (!user) return unauthorized();
  if (!user.isAdmin) return forbidden();

  const name = params.name;
  if (!name) return badRequest("Missing plugin name");

  const plugin = pluginManager.getPlugin(name);
  if (!plugin) return notFound("Plugin not found");

  const parsed = await parseBody(request, patchSchema);
  if ("error" in parsed) return parsed.error;

  try {
    if (parsed.data.config) {
      pluginManager.setPluginConfig(name, parsed.data.config);
    }

    if (parsed.data.action === "enable") {
      pluginManager.enablePlugin(name);
    } else if (parsed.data.action === "disable") {
      pluginManager.disablePlugin(name);
    } else if (parsed.data.action === "reload") {
      await pluginManager.reloadPlugin(name);
    } else if (parsed.data.action === "unload") {
      await pluginManager.unloadPlugin(name);
      return success({
        name,
        action: "unload",
        unloaded: true,
      });
    }
  } catch (error) {
    return badRequest(error instanceof Error ? error.message : "Plugin operation failed");
  }

  return success({
    name,
    action: parsed.data.action,
    runtime: pluginManager.getPluginState(name) || null,
    config: pluginManager.getPluginConfig(name),
  });
});